package cn.net.autocode.platform.table;

import cn.hutool.core.util.StrUtil;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.dbManager.SqlWrapper;
import cn.net.autocode.dbManager.SqlWrapperBuilder;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("platformTableRuleDetail")
@RequestMapping("/platform/tableRuleDetail")
public class TableRuleDetail {
    private final Dao dictDao;

    public TableRuleDetail(@Qualifier("dictDao") Dao dictDao) {
        this.dictDao = dictDao;
    }

    @RequestMapping("load")
    public ResultT load(){
       return null;
    }


    @RequestMapping("/change/{fieldName}")
    public ResultT change(@PathVariable("fieldName") String fieldName, @RequestBody JSONObject param){
        SqlWrapperBuilder swb = new SqlWrapperBuilder("ac_table_rules");
        if(StrUtil.isBlank(param.getString("id"))){
            return ResultT.success();
        }
        swb.eq("id", param.getString("id"));
        switch(fieldName){
            case "fieldName":
                swb.updateColumn("field_name=?",param.getString("fieldName"));
                break;
            case "refColumn":
                swb.updateColumn("ref_column=?",param.getJSONArray("refColumn").toJSONString());
                break;
            case "matchUl":
                swb.updateColumn("match_ul=?",param.getString("matchUl"));
                break;
            case "defaultValue":
                swb.updateColumn("default_value=?",param.getString("defaultValue"));
                break;
            case "resultType":
                swb.updateColumn("result_type=?",param.getString("resultType"));
                break;
            case "keyWord":
                swb.updateColumn("field_name=?",param.getString("keyWord"));
                break;
            default:
                return ResultT.success();
        }
        SqlWrapper sqlWrapper = swb.updateSqlWrapper();
        dictDao.executeUpdate(sqlWrapper.getSql(),sqlWrapper.getParams().toArray());
        dictDao.executeUpdate("update ac_table set is_sync='N' where id in (select table_id from ac_table_rules where id=?)",param.getString("id"));
        return ResultT.success();
    }


}
